package com.winit.flow.repository;

import com.winit.flow.model.Flow;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Repository
public class FlowRepositoryExt {
    @PersistenceContext
    private EntityManager entityManager;
    @Transactional
    public int updatePropertyNotNullById(Flow flow) {
        Flow entity = entityManager.find(Flow.class, flow.getId());
        if (null == entity){
            return 0;
        }
        boolean update = false;
        if (flow.getName() != null){
            entity.setName(flow.getName());
            update = true;
        }

        if (flow.getAttrConfigId() != null){
            entity.setAttrConfigId(flow.getAttrConfigId());
            update = true;
        }

        if (flow.getBussinessId() != null){
            entity.setBussinessId(flow.getBussinessId());
            update = true;
        }

        if (flow.getStatus() != null){
            entity.setStatus(flow.getStatus());
            update = true;
        }

        if (!update) {
            return 0;
        }
        entityManager.merge(entity);
        entityManager.flush();
        return 1;
    }
}
